package STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

import STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Entry.Bandoc;
import STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Entry.BangQLMuonSach;
import STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Entry.Sach;
import STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Models.Models;

public class Controllers {
	Models thuvienModels = new Models();

	/***
	 * Phuong thuc menu
	 */
	public void menu() {
		Scanner sc = new Scanner(System.in);
		int option = 0;
		Bandoc bandoc = null;

		// Danh sach sach

		Sach sach = null;

		// Bat dau chuong
		BangQLMuonSach objmuonsach = null;

		System.out.println("1.Quan ly dau sach");
		System.out.println("2.Quan ly ban doc");
		System.out.println("3.Quan ly muon sach");
		option = sc.nextInt();
		try {
			thuvienModels.loadData();
		} catch (JsonGenerationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (JsonMappingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		switch (option) {
		case 1:
			System.out.println("1.Danh sach dau sach");
			System.out.println("2.Nhap dau sach moi");

			sc = new Scanner(System.in);
			option = sc.nextInt();
			String stop = null;
			if (option == 1) {
				do {
					thuvienModels.getListSach();
					System.out.println("Ban co muon tiep tuc khong Y/N:");
					sc = new Scanner(System.in);
					stop = sc.nextLine();
					if (stop.equals("N")) {
						menu();
					}
				} while (stop != "N");
			} else {
				do {
					List<Sach> listSach = new ArrayList<Sach>();
					thuvienModels.nhapDanhSachDauSach(sach);
					thuvienModels.getListSach();
					sc = new Scanner(System.in);
					System.err.println("Ban co muon tiep tuc khong Y/N");
					stop = sc.nextLine();
					if (stop.equals("N")) {
						menu();
					}
				} while (stop != "Y");
			}

			break;
		case 2:

			System.out.println("1.Danh sach ban doc");
			System.out.println("2.Nhap ban doc moi");
			sc = new Scanner(System.in);
			option = sc.nextInt();
			if (option == 1) {
				String stop1;
				do {
					thuvienModels.getListBanDoc();
					sc = new Scanner(System.in);
					System.out.println("Ban co muon tiep tuc khong Y/N:");
					stop1 = sc.nextLine();
					if (stop1.equals("N")) {
						menu();
					}

				} while (stop1 != "Y");
			}
			if (option == 2) {
				String a = "";
				do {
					thuvienModels.Nhapdanhsachbandoc(bandoc);
					thuvienModels.getListBanDoc();
					sc = new Scanner(System.in);
					System.out.println("Ban co muon tiep tuc hay khong Y/N:");
					a = sc.nextLine();
					if (a.equals("N")) {
						menu();
					}
				} while (a != "Y");
			}

			break;
		case 3:
			System.out.println("----Quan li danh sach muon sach----");
			System.out.println("1.Danh sach muon sach");
			System.out.println("2.Them danh sach muon sach");
			System.out.println("3.Tim kiem danh sach muon sach");
			sc = new Scanner(System.in);
			int luachon = sc.nextInt();
			List<BangQLMuonSach> dstemp = new ArrayList<BangQLMuonSach>();
			if (luachon == 1) {
				String stop2 = null;
				do {
					System.out.println("Quan ly muon sach");
					thuvienModels.getListQuanlyMuonSach();
					System.out.println("Quay tro ve menu 1.Co/2.Khong");
					sc = new Scanner(System.in);
					int opt = sc.nextInt();
					if (opt == 1) {
						menu();
					}
				} while (stop2 != "Y");
			}
			if (luachon == 2) {
				try {
					thuvienModels.nhapDanhSachMuonSach(objmuonsach);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				thuvienModels.getListQuanlyMuonSach();
				System.out.println("Quay tro ve menu 1.Co/2.Khong");
				sc = new Scanner(System.in);
				int opt = sc.nextInt();
				if (opt == 1) {
					menu();
				}
			}
			if (luachon == 3) {
				List<BangQLMuonSach> temp = new ArrayList<BangQLMuonSach>();
				System.out.println("Nhap ten ban doc");
				String tenbandoc = null;
				sc = new Scanner(System.in);
				tenbandoc = sc.nextLine();
				temp = thuvienModels.findBandocs(tenbandoc);
				if (temp != null) {
					thuvienModels.getListQuanlyMuonSach();
				} else {
					System.out.println("Nhap 0 de quay lai menu");
					sc = new Scanner(System.in);
					int i = sc.nextInt();
					if (i == 0) {
						System.out.println("------------------------");
						menu();
					}
				}

			}
			break;

		default:
			break;
		}

	}

}
